#region Usings
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using LimitlessConnector;
using System;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Navigation;
using LimitlessLightSwitch.Helper;
using Microsoft.Phone.Controls;
using LimitlessLightSwitch.Resources; 
#endregion

namespace LimitlessLightSwitch.ViewModel
{

  /// <summary>
  /// This class contains properties that the main View can data bind to.
  /// <para>
  /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
  /// </para>
  /// <para>
  /// You can also use Blend to data bind with the tool's support.
  /// </para>
  /// <para>
  /// See http://www.galasoft.ch/mvvm
  /// </para>
  /// </summary>
  public class RGBWRemoteViewModel : ViewModelBase
  {

    #region Needs
    private int m_LastSelectedGroup = 1;
    private IRGBWConnector m_RGBWConnector;
    private int m_CurrentBrightness = 2;
    private int m_CurrentColor = 0;
    #endregion

    #region Constructor
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public RGBWRemoteViewModel()
    {
      String HostName = null;
      String Port = null;
      if (IsolatedStorageSettings.ApplicationSettings.Contains("HostNameSetting"))
        HostName = IsolatedStorageSettings.ApplicationSettings["HostNameSetting"].ToString();
      if (IsolatedStorageSettings.ApplicationSettings.Contains("PortSetting"))
        Port = IsolatedStorageSettings.ApplicationSettings["PortSetting"].ToString();
      m_RGBWConnector = new RGBWConnector(HostName, Port);
    } 
    #endregion

    #region Commands
    private RelayCommand m_EnableAllLights;

    /// <summary>
    /// Gets the LightsToggle.
    /// </summary>
    public RelayCommand EnableAllLights
    {
      get
      {
        return m_EnableAllLights
            ?? (m_EnableAllLights = new RelayCommand(
                                  async () =>
                                  {
                                    if (!Connector.HasNetworkSettings)
                                      return;
                                    await Connector.EnableAll();
                                  }));
      }
    }


    private RelayCommand m_DisableAllLights;

    /// <summary>
    /// Gets the DisableAllLights.
    /// </summary>
    public RelayCommand DisableAllLights
    {
      get
      {
        return m_DisableAllLights
            ?? (m_DisableAllLights = new RelayCommand(
                                  async () =>
                                  {
                                    if (!Connector.HasNetworkSettings)
                                      return;
                                    await Connector.DisableAll();
                                  }));
      }
    }


    private RelayCommand<object> m_EnableGroup;

    /// <summary>
    /// Gets the EnableGroup.
    /// </summary>
    public RelayCommand<object> EnableGroup
    {
      get
      {
        return m_EnableGroup
            ?? (m_EnableGroup = new RelayCommand<object>(
                                  async GroupNo =>
                                  {
                                    if (!Connector.HasNetworkSettings)
                                      return;
                                    m_LastSelectedGroup = GroupNo.ToInt32();
                                    await Connector.EnableGroup(GroupNo.ToInt32());
                                  }));
      }
    }

    private RelayCommand<object> m_DisableGroup;

    /// <summary>
    /// Gets the DisableGroup.
    /// </summary>
    public RelayCommand<object> DisableGroup
    {
      get
      {
        return m_DisableGroup
            ?? (m_DisableGroup = new RelayCommand<object>(
                                  async GroupNo =>
                                  {
                                    if (!Connector.HasNetworkSettings)
                                      return;
                                    await Connector.DisableGroup(GroupNo.ToInt32());
                                  }));
      }
    }


    private RelayCommand m_SetWhiteColor;

    /// <summary>
    /// Gets the SetWhiteColor.
    /// </summary>
    public RelayCommand SetWhiteColor
    {
      get
      {
        return m_SetWhiteColor
            ?? (m_SetWhiteColor = new RelayCommand(
                                  () =>
                                  {
                                    SetWhite(m_LastSelectedGroup);
                                  }));
      }
    } 
    #endregion

    #region Properties


    internal IRGBWConnector Connector
    {
      get
      {
        if (!m_RGBWConnector.HasNetworkSettings)
        {
          MessageBox.Show(AppResources.MessageSetUpWifiBridge);
          var Root = ((PhoneApplicationFrame)App.Current.RootVisual);
          Root.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative), m_RGBWConnector);
        }
        return m_RGBWConnector;
      }
    }

    /// <summary>
    /// Sets and gets the CurrentBrightness property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int CurrentBrightness
    {
      get
      {
        return m_CurrentBrightness;
      }

      set
      {
        if (m_CurrentBrightness == value)
        {
          return;
        }
        RaisePropertyChanging(() => CurrentBrightness);
        m_CurrentBrightness = value;
        RaisePropertyChanged(() => CurrentBrightness);
        SetBrightness(m_LastSelectedGroup, m_CurrentBrightness);
      }
    }

    /// <summary>
    /// Sets and gets the CurrentColor property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int CurrentColor
    {
      get
      {
        return m_CurrentColor;
      }

      set
      {
        if (m_CurrentColor == value)
        {
          return;
        }

        RaisePropertyChanging(() => CurrentColor);
        m_CurrentColor = value;
        RaisePropertyChanged(() => CurrentColor);
        SetColor(m_LastSelectedGroup, m_CurrentColor);
      }
    } 
    #endregion

    #region Private methods
    private async void SetBrightness(int Group, int Brightness)
    {
      if (!Connector.HasNetworkSettings)
        return;
      await Connector.SetBrightness(Group, Brightness);
    }

    private async void SetColor(int Group, int Color)
    {
      if (!Connector.HasNetworkSettings)
        return;
      await Connector.SetColor(Group, Color);
    }

    private async void SetWhite(int Group)
    {
      if (!Connector.HasNetworkSettings)
        return;
      await Connector.SetWhite(Group);
    } 
    #endregion


  }
}
